using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
 
namespace Web {
	public class MvcApplication : System.Web.HttpApplication {
		protected void Application_Start() {
			AreaRegistration.RegisterAllAreas();

			WebApiConfig.Register(GlobalConfiguration.Configuration);
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			BundleConfig.RegisterBundles(BundleTable.Bundles);
			AuthConfig.RegisterAuth();
		}


		protected void Application_AcquireRequestState(object sender, EventArgs e) {
			if (HttpContext.Current.Session != null) {
				CultureInfo cultureInfo = (CultureInfo)this.Session[Nebulosa.Core.Constants.SessionCulture];

				if (cultureInfo == null) {
					string languageCode = Nebulosa.Core.Constants.DefaultCulture;
					if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0) {
						languageCode = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
					}

					cultureInfo = new CultureInfo(languageCode);
					this.Session[Nebulosa.Core.Constants.SessionCulture] = cultureInfo;
				}

				Thread.CurrentThread.CurrentUICulture = cultureInfo;
				Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
			}
		}
	}
}